﻿using FytSoa.Comment.Utils;
using FytSoa.Common.Captcha;
using FytSoa.Common.Utils;
using FytSoa.Domain.Core.Cache;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FytSoa.ApiService.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CaptchaController : ControllerBase
    {
        private readonly SecurityCode _securityCode;
        public CaptchaController(SecurityCode securityCode)
        {
            _securityCode = securityCode;
        }

        [HttpGet("{identify}")]
        public IActionResult Get(string identify)
        {
            if (string.IsNullOrEmpty(identify))
            {
                identify = CommonUtils.GetIp();
            }
            var code = _securityCode.GetRandomEnDigitalText(4);
            MemoryService.Default.SetCache(KeyUtils.CAPTCHACODE + identify, code,5);
            var imgbyte = _securityCode.GetGifEnDigitalCodeByte(code);
            return File(imgbyte, "image/gif");

        }
    }
}
